/*
    Copyright (c) 2006-2007 Ivaylo Ivanov

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.
*/
package com.avalonsql.core.db.managers.impl;

import com.avalonsql.core.db.DBException;
import com.avalonsql.core.db.DBManager;
import com.avalonsql.core.db.connection.ConnectionWrapper;
import com.avalonsql.core.db.managers.api.IPrimaryKeysManager;
import com.avalonsql.core.db.objects.impl.PrimaryKey;
import com.avalonsql.core.db.objects.impl.Table;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.TreeSet;

public class PrimaryKeysManager extends ConnectionWrapperManager implements IPrimaryKeysManager {
    
    public PrimaryKeysManager(ConnectionWrapper connectionWrapper) {
        super(connectionWrapper);
    }

    public Collection<PrimaryKey> loadAll(Table parent) throws DBException {
        if (parent == null) {
            throw new IllegalArgumentException("parent = null");
        }

        Collection<PrimaryKey> values = new TreeSet<PrimaryKey>();
        ResultSet rs = null;
        try {
            rs = getDatabaseMetaData().getPrimaryKeys(parent.getCatalog(), parent.getSchema(), parent.getName());
            while (rs.next()) {
                String keyName = getStringValue(rs, "PK_NAME");
                if (keyName != null) {
                    values.add(new PrimaryKey(getStringValue(rs, "TABLE_SCHEM"), getStringValue(rs, "TABLE_CAT"), keyName, parent));
                }
            }
        } catch (SQLException e) {
            throw new DBException(e);
        } finally {
            DBManager.closeResultSet(rs);
        }
        return values;
    }

    public void create(PrimaryKey dbObject) throws DBException {
        //todo
    }

    public void update(PrimaryKey dbObject) throws DBException {
        //todo
    }

    public void delete(PrimaryKey dbObject) throws DBException {
        //todo
    }
}
